/**
 * @file lv_watch_activity_obj.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_ACTIVITY_OBJ != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/


/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * lv_watch_creat_activity_obj(lv_watch_activity_ext_t * actvity_ext)
{
    lv_obj_t * obj = lv_obj_create(lv_scr_act(), NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_obj_set_size(obj, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_local_bg_color(obj, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);

    lv_watch_activity_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_watch_activity_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    memset(ext, 0, sizeof(lv_watch_activity_ext_t));

    ext->actId = actvity_ext->actId;   /*activity id of the obj*/
    ext->create = actvity_ext->create;
    ext->prepare_destory =  actvity_ext->prepare_destory; /*do before del obj*/

#if USE_LV_WATCH_LANG_ARABIC != 0
    if(lv_lang_act() == LANG_AR) {
        lv_obj_set_base_dir(obj, LV_BIDI_DIR_RTL);
    }
#endif

    return obj;
}

lv_obj_t * lv_watch_get_top_activity_obj(void)
{
    lv_obj_t * par = lv_scr_act();
    lv_obj_t * obj = _lv_ll_get_head(&(par)->child_ll);
    LV_ASSERT_MEM(obj);
    //printf("lv_watch_get_top_activity_obj, obj=%p\n", obj);
    return obj;
}

lv_obj_t * lv_watch_get_activity_obj(lv_watch_Activity_Id_t actId)
{
    lv_obj_t * par = lv_scr_act();
    lv_obj_t * obj;

    _LV_LL_READ((par)->child_ll, obj) {
        lv_watch_activity_ext_t * ext = lv_obj_get_ext_attr(obj);
        if(ext == NULL) continue;

        if(ext->actId == actId) {
            //printf("lv_watch_get_activity_obj, obj=%p, actId=%d\n", obj, actId);
            return obj;
        }
    }
    return NULL;
}

void lv_watch_dump_activity_obj(void)
{
    lv_obj_t * par = lv_scr_act();
    lv_obj_t * obj;

    _LV_LL_READ((par)->child_ll, obj) {
        lv_watch_activity_ext_t * ext = lv_obj_get_ext_attr(obj);
        if(ext == NULL) continue;
        printf("lv_watch_dump_activity_obj, obj=%p, actId=%d\n", obj, ext->actId);
    }
}

#if 0
lv_obj_t * lv_watch_get_bg_activity_obj(lv_watch_Activity_Id_t actId)
{
    lv_obj_t * par = lv_scr_act();
    lv_obj_t * obj;

    LL_READ((par)->child_ll, obj) {
        lv_watch_activity_ext_t * ext = lv_obj_get_ext_attr(obj);
        if(ext == NULL) continue;

        if(ext->actId == actId) {
            obj = lv_ll_get_next(&(par)->child_ll, obj);
            printf("lv_watch_get_bg_activity_obj, obj=%p, actId=%d\n", obj, actId);
            return obj;
        }
    }
    return NULL;
}
#endif

lv_obj_t * lv_watch_get_bg_activity_obj(void)
{
    lv_obj_t * par = lv_scr_act();
    lv_obj_t * obj = _lv_ll_get_head(&(par)->child_ll);
    obj = _lv_ll_get_next(&(par)->child_ll, obj);
    LV_ASSERT_MEM(obj);
    printf("lv_watch_get_bg_activity_obj, obj=%p\n", obj);
    return obj;
}

/**********************
 *   STATIC FUNCTIONS
***********************/

#endif /*USE_LV_WATCH_ACTIVITY_OBJ*/
